//
//  Part.m
//  MailDelivery
//
//  Created by Dante Palacios on 9/26/09.
//  Copyright 2009 Dante Palacios. All rights reserved.
//

#import "Part.h"

#import "Funtions.h"
#import "Macros.h"


@implementation Part

@synthesize name = _name;
@synthesize contentType = _contentType;
@synthesize contentID = _contentID;
@synthesize contentDisposition = _contentDisposition;
@synthesize contentTransferEncoding = _contentTransferEncoding;
@synthesize filename = _filename;
@synthesize format = _format;
@synthesize boundary = _boundary;
@synthesize protocol = _protocol;
@synthesize charset = _charset;
@synthesize content = _content;
@synthesize headers = _headers;

- (id)copyWithZone:(NSZone *)zone 
{
	Part *part = [[[self class] allocWithZone:zone] init];
	part.content = self.content;
	part.headers = self.headers;
	part.name = self.name;
	part.contentType = self.contentType;
	part.contentID = self.contentID;
	part.contentDisposition = self.contentDisposition;
	part.contentTransferEncoding = self.contentTransferEncoding;
	part.filename = self.filename;
	part.format = self.format;
	//part.boundary = _boundary;
	part.protocol = self.protocol;
	part.charset = self.charset;
	return part;
}

- (void)encodeWithCoder:(NSCoder *)coder 
{
	[coder encodeObject:self.content forKey:@"content"];
	[coder encodeObject:self.headers forKey:@"headers"];
	[coder encodeObject:self.name forKey:@"name"];
	[coder encodeObject:self.contentType forKey:@"contentType"];
    [coder encodeObject:self.contentID forKey:@"contentID"];
    [coder encodeObject:self.contentDisposition forKey:@"contentDisposition"];
	[coder encodeObject:self.contentTransferEncoding forKey:@"contentTransferEncoding"];
	[coder encodeObject:self.filename forKey:@"filename"];
	[coder encodeObject:self.format forKey:@"format"];
    [coder encodeObject:self.boundary forKey:@"boundary"];
    [coder encodeObject:self.protocol forKey:@"protocol"];
	[coder encodeObject:self.charset forKey:@"charset"];
}

- (id)initWithCoder:(NSCoder *)decoder 
{
	Part *part = [[[self class] alloc] init];
	part.content = [decoder decodeObjectForKey:@"content"];
	part.headers = [decoder decodeObjectForKey:@"headers"];
	part.name = [decoder decodeObjectForKey:@"name"];
	part.contentType = [decoder decodeObjectForKey:@"contentType"];
	part.contentID = [decoder decodeObjectForKey:@"contentID"];
	part.contentDisposition = [decoder decodeObjectForKey:@"contentDisposition"];
	part.contentTransferEncoding = [decoder decodeObjectForKey:@"contentTransferEncoding"];
	part.filename = [decoder decodeObjectForKey:@"filename"];
	part.format = [decoder decodeObjectForKey:@"format"];
	part.boundary = [decoder decodeObjectForKey:@"boundary"];
	part.protocol = [decoder decodeObjectForKey:@"protocol"];
	part.charset = [decoder decodeObjectForKey:@"charset"];
	
    return part;
}

- (void)dealloc 
{
	[_content release];
	_content = nil;
	[_name release];
	_name = nil;
	[_contentType release];
	_contentType = nil;
	[_contentID release];
	_contentID = nil;
	[_contentDisposition release];
	_contentDisposition = nil;
	[_contentTransferEncoding release];
	_contentTransferEncoding = nil;
	[_filename release];
	_filename = nil;
	[_format release];
	_format = nil;
	[_boundary release];
	_boundary = nil;
	[_protocol release];
	_protocol = nil;
	[_charset release];
	_charset = nil;
	[_headers release];
	_headers = nil;
	
	[super dealloc];
}

- (id)content 
{
    return AtomicAutoreleasedGet(_content);
}

- (void)setContent:(id)value 
{
	AtomicRetainedSet(_content, value);
}

- (NSString *)contentType 
{
   return AtomicAutoreleasedGet(_contentType);
}

- (void)setContentType:(NSString *)value 
{
    AtomicCopiedSet(_contentType, value);
}

- (NSString *)contentID 
{
    return AtomicAutoreleasedGet(_contentID);
}

- (void)setContentID:(NSString *)value 
{
    AtomicCopiedSet(_contentID, value);
}

- (NSString *)contentDisposition 
{
    return AtomicAutoreleasedGet(_contentDisposition);
}

- (void)setContentDisposition:(NSString *)value 
{
    AtomicCopiedSet(_contentDisposition, value);
}

- (NSString *)contentTransferEncoding 
{
    return AtomicAutoreleasedGet(_contentTransferEncoding);
}

- (void)setContentTransferEncoding:(NSString *)value 
{
     AtomicCopiedSet(_contentTransferEncoding, value);
}

- (NSString *)name 
{
    return AtomicAutoreleasedGet(_name);
}

- (void)setName:(NSString *)value 
{
     AtomicCopiedSet(_name, value);
}

- (NSString *)filename 
{
    return AtomicAutoreleasedGet(_filename);
}

- (void)setFilename:(NSString *)value 
{
    AtomicCopiedSet(_filename, value);
}

- (NSString *)format 
{
    return AtomicAutoreleasedGet(_format);
}

- (void)setFormat:(NSString *)value 
{
    AtomicCopiedSet(_format, value);
}

- (NSString *)boundary 
{
	NSString *value = nil;
	@synchronized(self) 
	{
		if (!_boundary) 
		{
			NSInteger partNumber = (random() % 11) + 4;
			NSString *randomBoundary = [NSString stringWithFormat:@"%d", random()];
			NSString *bundleName = [[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *) kCFBundleNameKey] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
			if (!bundleName) bundleName = [[NSBundle bundleForClass:[Part class]] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
			
			_boundary = [[NSString stringWithFormat:@"%@-%d--%@", bundleName, partNumber, randomBoundary] retain];
		}
		value = [[_boundary retain] autorelease];
	}
	
	return value;
}

- (void)setBoundary:(NSString *)value 
{
     AtomicRetainedSet(_boundary, value);
}

- (NSString *)protocol 
{
    return AtomicAutoreleasedGet(_protocol);
}

- (void)setProtocol:(NSString *)value 
{
     AtomicCopiedSet(_protocol, value);
}

- (NSString *)charset 
{
    return AtomicAutoreleasedGet(_charset);
}

- (void)setCharset:(NSString *)value 
{
    AtomicCopiedSet(_charset, value);
}

- (NSDictionary *)headers 
{
    return AtomicAutoreleasedGet(_headers);
}

- (void)setHeaders:(NSDictionary *)value 
{
    AtomicRetainedSet(_headers, value);
}

@end
